package com.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dto.UserDTO;
import com.entity.IssueEntity;
import com.service.ExcelReportService;
import com.service.UserService;
import com.service.status.StatusCode;

@Component
@ManagedBean
@SessionScoped
public class UserController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3765505559700029898L;

	@Autowired
	private UserService userService;
	@Autowired
	private ExcelReportService excelReportService;

	@Autowired
	private TicketController ticket;

	private List<UserDTO> user;
	private String userEmail;
	private List<IssueEntity> issues;
	private StreamedContent file;
	private TreeNode root;
	private String status = "All";

	public void getReport() {
		ticket.setShowTicketTable(false);
		if (userEmail == "") {
			FacesContext.getCurrentInstance().addMessage("developersEmail", new FacesMessage("Please select developer"));
		} else {
			switch (StatusCode.valueOf(status)) {

			case Resolved: {
				getDeveloperTicketsByStatus(StatusCode.Resolved, StatusCode.Closed);
				break;
			}
			case All: {
				getAllDeveloperTickets();
				break;
			}

			default: {
				getDeveloperTicketsByStatus(StatusCode.InProgress, StatusCode.Open);
			}
			}
		}
	}

	public void getAllDeveloperTickets() {
		issues = userService.getIssuesByDevelopersEmail(userEmail);
		addUserInfoNodes(issues);
		excelReportService.createReducedIssueTreeTable(root, issues);
		excelReportService.createReportPoiByDeveloper(issues);
	}

	public void getDeveloperTicketsByStatus(StatusCode... codes) {
		issues = userService.getIssuesByDevelopersEmailAndStatus(userEmail, codes);
		addUserInfoNodes(issues);
		excelReportService.createReducedIssueTreeTable(root, issues);
		excelReportService.createReportPoiByDeveloper(issues);
	}

	public List<UserDTO> getAllUsers() {
		return userService.getAllUsers();
	}

	public List<UserDTO> getUser() {
		return user;
	}

	public void setUser(List<UserDTO> user) {
		this.user = user;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public List<IssueEntity> getIssues() {
		return issues;
	}

	public void setIssues(List<IssueEntity> issues) {
		this.issues = issues;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	private void addUserInfoNodes(List<IssueEntity> issues) {
		root = new DefaultTreeNode("root", null);
		UserDTO user = userService.getUserByEmail(userEmail);
		excelReportService.createTreeNode("Name", user.getDisplayName(), root);
		excelReportService.createTreeNode("Self", user.getSelf(), root);
		excelReportService.createTreeNode("Email", user.getEmailAddress(), root);
		excelReportService.createTreeNode("Active", user.getActive(), root);
		excelReportService
				.createTreeNode("Total timespent", excelReportService.getTimeDuration(excelReportService.countTotalTimespent(issues)), root);
	}

	public TicketController getTicket() {
		return ticket;
	}

	public void setTicket(TicketController ticket) {
		this.ticket = ticket;
	}

}
